package com.kyrie.lib_sys.application.bookApplys.controller;

import com.google.common.base.Preconditions;
import com.kyrie.lib_sys.annotation.logAnnotation.OperationLog;
import com.kyrie.lib_sys.annotation.logAnnotation.OperationType;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.entity.Result;
import com.kyrie.lib_sys.common.pojo.dto.BookDTO;
import com.kyrie.lib_sys.common.pojo.dto.BookPageQueryDTO;
import com.kyrie.lib_sys.application.bookApplys.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书相关controller类
 *
 * @author heefM
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/book/")
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 新增图书
     * @param bookDTO
     * @return
     */
    @OperationLog(type = OperationType.INSERT)
    @RequestMapping("/add")
    public Result addBook(@RequestBody BookDTO bookDTO) {
        if (log.isInfoEnabled()) {
            log.info("添加图书Controller:addBook: {}", bookDTO);
        }
        Preconditions.checkArgument(!StringUtils.isBlank(bookDTO.getIsbn()), "图书编号不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(bookDTO.getTitle()), "图书名称不能为空");

        bookService.insert(bookDTO);
        return Result.success("添加图书成功");
    }


    /**
     * 根据id查询图书
     *
     * @param id
     * @return
     */
    @OperationLog(type =OperationType.QUERY)
    @RequestMapping("/queryById")
    public Result<BookDTO> queryBookById(@RequestParam Long id) {
        log.info("根据id查询图书: {}", id);
        Preconditions.checkNotNull(id, "图书id不能为空");
        BookDTO bookDTO = bookService.queryBookById(id);

        return Result.success(bookDTO);
    }

    /**
     * 分页查询所有图书
     *
     * @return
     */
    @RequestMapping("/list")
    public Result<PageResult> queryPage(BookPageQueryDTO bookPageQueryDTO) {

        log.info("分页查询所有图书: {}", bookPageQueryDTO);
        // 设置默认分页参数
        if (bookPageQueryDTO.getPageNum() == null) {
            bookPageQueryDTO.setPageNum(1);
        }
        if (bookPageQueryDTO.getPageSize() == null) {
            bookPageQueryDTO.setPageSize(10);
        }
        PageResult pageResult = bookService.queryPage(bookPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 条件查询图书
     *
     * @param bookDTO
     * @return
     */
    @RequestMapping("/queryByCondition")
    public Result<BookDTO> queryByCondition(@RequestBody BookDTO bookDTO) {
        if (log.isInfoEnabled()) {
            log.info("条件查询图书: {}", bookDTO);
        }
        bookDTO = bookService.queryByCondition(bookDTO);
        return Result.success(bookDTO);
    }


    /**
     * 修改图书
     *
     * @param bookDTO
     * @return
     */
    @RequestMapping("/update")
    public Result updateBook(@RequestBody BookDTO bookDTO) {
        if (log.isInfoEnabled()) {
            log.info("修改图书Controller:updateBook: {}", bookDTO);
        }
        Preconditions.checkArgument(bookDTO != null, "图书信息不能为空");
        bookService.update(bookDTO);
        return Result.success("修改图书成功");
    }

    /**
     * 根据id删除图书
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result<String> deleteBook(@RequestParam("id") Long id) {
        if (log.isInfoEnabled()) {
            log.info("删除图书Controller:deleteBook: {}", id);
        }
        bookService.deleteById(id);
        return Result.success("删除图书成功");
    }

    /**
     * 批量删除 图书
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestParam List<Long> ids) {
        if (log.isInfoEnabled()) {
            log.info("批量删除图书Controller:deleteBatch: {}", ids);
        }
        bookService.deleteBatch(ids);
        return Result.success("批量删除图书成功");
    }


    /**
     * 批量新增 图书
     *
     * @param bookDTOS
     * @return
     */
    @RequestMapping("/insertBatch")
    public Result insertBatch(@RequestBody List<BookDTO> bookDTOS) {
        if (log.isInfoEnabled()) {
            log.info("批量新增图书insertBatch: {}", bookDTOS);
        }
        Preconditions.checkArgument(bookDTOS != null && bookDTOS.size() > 0, "图书列表不能为空");
        bookService.insertBatch(bookDTOS);
        return Result.success("批量新增图书成功");
    }


}
